<?php
/**
 * 《固定资产》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */

require_once(dirname(__FILE__) . '/FinanceUtil.class.php');

class Asset extends FinanceUtil {
	public $tbl = Constant::tbl_cw_asset;
	
	public $unit = array(
		'-' => '请选择',
		'个' => '个',
		'台' => '台',
		'件' => '件',
		'块' => '块',
		'把' => '把',
		'千克' => '千克',
		'公升' => '公升',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '闲置未用',
		'2' => '正在使用',
		'3' => '已经报废',
		'4' => '不能使用',

		'5' => '已经打印',
		'6' => '已经签字',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '名称',
		'category' => '大类',
	
		'dept' => '部门',
		'emp' => '使用人',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'emp' => '使用人',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct();
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$category = $this->_category();
		$controller->assign_by_ref('category_option', $category);
		
		$controller->assign_by_ref('unit_option', $this->unit);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'固定资产' => array('cc'=>'finance.php?do=Asset.find', 'js'=>'1'),
			'资产明细' => array('cc'=>'finance.php?do=AssetItem.find', 'js'=>'1'),
			'资产交接' => array('cc'=>'finance.php?do=AssetUse.find', 'js'=>'1'),
			// '报废信息' => array('cc'=>'finance.php?do=Scrap.find', 'js'=>'1'),
		
			'出库明细' => array('cc'=>'material.php?do=Out.find', 'js'=>''),
			'物料库存' => array('cc'=>'material.php?do=Amount.find', 'js'=>''),
		
			// '固定资产2' => array('cc'=>'info.php?do=Equipment.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '责任人',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'finance/asset/find');
		return true;
	}

	function _category() {
		$key = "asset.category_";
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT category FROM %s WHERE length(category) > 0";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$ret[$row['category']] = $row['category'];
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function _type() {
		$key = "asset.type_";
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT type FROM %s WHERE length(type) > 0";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$ret[$row['type']] = $row['type'];
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_category($controller, $request) {
		if (!empty($request['category'])) {
			$category = $request['category'];
		}

		if (!empty($request['category_s'])) {
			$category = $request['category_s'];
		}

		if (!empty($request['category_t'])) {
			$category = $request['category_t'];
		}

		if (empty($category) || $category == '-') {
			$data[] = array('code'=>'-', 'name'=>'请选择');
			echo json_encode($data);
			return false;
		}

		$key = "asset.category_" . substr(md5($category), 0, 10);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				echo json_encode($ret);
				return true;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT type FROM %s WHERE category = '%s'";
		$sql = sprintf($fmt, $this->tbl, $category);
		debug($sql);
		$result = $db->query($sql);
		$ret[] = array('code'=>'-', 'name'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$row['code'] = $row['type'];
			$row['name'] = $row['type'];

			$ret[] = $row;
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("3 day"));
		}

		echo json_encode($ret);
		return true;
	}

	/**
	 * 清除缓存
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_category_type($controller, $request) {

		$category = $this->_category();
		while (list($k, $v) = each($category)) {
			$key = "asset.category_" . substr(md5($k), 0, 10);
			clean($key);
		}

		$key = "asset.category_";
		clean($key);

		$key = "asset.type_";
		clean($key);

		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'dept') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = Name::dept($row['dept']);
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$name_s = $request['name_s'];

		$category_s = $request['category_s'];
		$type_s = $request['type_s'];
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		} 

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($name_s)) {
			$where .= " AND (name like '%" . $name_s . "%' OR content like '%" . $name_s . "%')";
		}
		
		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}		

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		$emp = $_SESSION['id'];
		
		$p = array('1'=>'153', '2'=>'154', '3'=>'172');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
			
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}

		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['num_'] = $this->_num($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
		
		$controller->display($request, 'finance/asset/list');
		return true;
	}
	
	function _num($id) {
		require_once(dirname(__FILE__) . '/AssetItem.class.php');
		$aa = new AssetItem();
	
		return $aa->_count($id);
	}

	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
		
		if ($request['xls'] == '2') {
			$controller->display($request, 'finance/asset/list_2');
		} elseif ($request['xls'] == '3') {
			$controller->display($request, 'finance/asset/list_3');
		} else {
			$controller->display($request, 'finance/asset/list');
		}
		
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$row['item'] = json_encode($this->_item_blank(2));
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/asset/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$name = $request['name'];
		$location = addslashes($request['location']);

		$category = addslashes($request['category']);
		$type = addslashes($request['type']);

		$date = $request['date'];
		$content = addslashes($request['content']);
		$status = $request['status'];
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, name, location, category, type, date, content, oper, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $name, $location, $category, $type, $date, $content, $_SESSION['id'], $status);
		debug($sql);
		$db->query($sql);
		
		$id = $db->insert_id();
		$request['id_s'] = $id;
		$request['id'] = $id;
		
		$this->_insert_item($request);		

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _insert_item($request) {
		require_once(dirname(__FILE__) . '/AssetItem.class.php');
		$aa = new AssetItem();
	
		return $aa->_insert($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['item'] = json_encode($this->_item($row['id']));

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/asset/edit');
		return true;
	}
	
	function _item($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/AssetItem.class.php');
		$aa = new AssetItem();
	
		return $aa->_list($id);
	}
	
	function _use($row, $status = 'all') {
		require_once(dirname(__FILE__) . '/AssetUse.class.php');
		$aa = new AssetUse();
	
		$ret = $aa->_list($row['id']);
		$i = count($ret);
		
		while ($i < 12) {
			if ($i == 0) {
				$ret[] = array('date'=>$row['date'], 'accepter_t'=>$row['emp_t']);
			} else {
				$ret[] = array();
			}
			
			$i++;
		}
		
		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$name = $request['name'];
		$location = addslashes($request['location']);

		$category = addslashes($request['category']);
		$type = addslashes($request['type']);

		$date = $request['date'];
		$content = addslashes($request['content']);
		$status = $request['status'];
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET name = '%s', location = '%s', category = '%s', type = '%s', ";
		$fmt .= "date = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $location, $category, $type, $date, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$this->_update_item($request);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_item($request) {
		require_once(dirname(__FILE__) . '/AssetItem.class.php');
		$aa = new AssetItem();
	
		return $aa->_update($request);
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/asset/upload');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['item'] = $this->_item($row['id']);
		
		$row['use'] = $this->_use($row);

		$controller->assign_by_ref('result', $row);
		
		if ($request['view'] == '2') {
			$controller->display($request, 'finance/asset/view_2');
		} elseif ($request['view'] == '3') {
			$controller->display($request, 'finance/asset/view_3');
		} else {
			$controller->display($request, 'finance/asset/view');
		}
		
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['files'] = $this->_files($row['id']);
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['item'] = json_encode($this->_item($row['id']));

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/asset/copy');
		return true;
	}
	
	function transfer($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/asset/transfer');
		return true;
	}
	
	function transferPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$date = $request['date'];
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		
		$accepter = $this->_input($request['accepter']);
		$office = $this->_office($accepter);
		$dept = $this->_emp_dept($accepter);
		
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $accepter, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$this->_transfer($emp, $accepter, $id, $date, $content);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		
		$this->findPost($controller, $request);
		return true;
	}
	
	function _transfer($emp, $accepter, $asset, $date, $content) {
		
		require_once(dirname(__FILE__) . '/AssetUse.class.php');
		$aa = new AssetUse();
		
		$aa->_insert($emp, $accepter, $asset, $date, $content);
	}
	
	function scrap($controller, $request) {
		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/scrap/copy');
		return true;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}
		
		if ($this->_audit('153') || $this->_audit('154')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function ajax_b($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
	
		if ($this->_ajax_x($id, 'emp')) {
			$ret['status'] = 1;
		} elseif ($this->_ajax_x($id, 'oper')) {
			$ret['status'] = 1;
		} elseif ($this->_audit('153')) {
			$ret['status'] = 1;
		} elseif ($this->_audit('154')) {
			$ret['status'] = 1;
		} elseif ($this->_audit('172')) {
			$ret['status'] = 1;
		} elseif ($this->_is_admin()) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}
	
		echo json_encode($ret);
	}

	function z($controller, $request) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE id = 955";
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql;
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			// print_r($row);

			$aa = preg_split("/[\\n]+/i", $row['content']);
			
			// print_r($aa);
			$b = array();
			while (list($k, $v) = each($aa)) {
				if (empty($v)) {
					continue;
				}
				
				$b[] = array('name'=>$v, 'content'=>'', 'ok'=>'true');
			}
			
			print_r($b);
			
			$this->_insert_item($row['id'], $b, $row['emp']);
		}

		logdate($controller, $request);
		return true;
	}
	
	function tj1($controller, $request) {
		$a = array(
			'固定资产（南山）'=>array('a'=>'330', 'office'=>'1'),
			'固定资产（沙井）'=>array('a'=>'331', 'office'=>'2'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
						
			$this->tj($controller, $request);
		}
	}
	
	function tj2($controller, $request) {
		$a = array(
			'固定资产（南山）周报'=>array('a'=>'330', 'office'=>'1'),
			'固定资产（沙井）周报'=>array('a'=>'331', 'office'=>'2'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
						
			$this->tjw($controller, $request);
		}
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = $request['title'];
		$office = $request['office'];
		
		$cc = array(	
			'新增信息'=>array('w' => "office = '$office' AND DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'status'),
			'更新信息'=>array('w' => "office = '$office' AND DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}
		
		$controller->clear_assign('result');
		
		$body = $this->_tj_html($controller, $request, 'finance/asset/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjw($controller, $request) {
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = $request['title'];
		$office = $request['office'];
		
		$cc = array(	
			'新增信息'=>array('w' => "office = '$office' AND DATE_FORMAT(createtime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'status'),
			'更新信息'=>array('w' => "office = '$office' AND DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE office = '$office' AND modifytime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE office = '$office' AND modifytime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE office = '$office' AND modifytime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE office = '$office' AND modifytime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE office = '$office' AND modifytime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);

		$body = $this->_tj_html($controller, $request, 'finance/asset/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'emp';
			
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}		
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}
	
	function move($from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s' AND status = 2";
		$sql = sprintf($fmt, $this->tbl, $to, $from);
		debug($sql);
		$db->query($sql);
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'finance/asset/_list_c');
		return true;
	}
}
?>
